package com.csii.adhoc.JdbcTemplate;

import cn.hutool.core.util.StrUtil;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 数据库工具类
 * @author: panChuanJiang
 * @create: 2021-03-30 10:34
 */
public final class DbUilts {
    /**
     * 将数据库字段转成驼峰命名
     *
     * @param dbFiled 数据库下划线字段
     * @return 驼峰命名
     */
    public static String getHumpName(String dbFiled) {
        return StrUtil.toCamelCase(dbFiled);
    }

    /**
     * @param record
     * @return
     */
    public static List<Map<String, Object>> setHumpNameKey(List<Map<String, Object>> record) {
        if (record == null || record.size() == 0) {
            return null;
        }
        Map<String, String> trans = new HashMap<>();
        Map<String, Object> map = record.get(0);
        int size = map.size();
        map.entrySet().forEach(x -> {
            trans.put(x.getKey(), StrUtil.toCamelCase(x.getKey().toLowerCase()));
        });
        List<Map<String, Object>> res = new ArrayList<>(record.size());
        record.forEach(x -> {
            HashMap<String, Object> temp = new HashMap(size);
            x.entrySet().forEach(y -> {
                temp.put(trans.get(y.getKey()), y.getValue());
            });
            res.add(temp);
        });
        return res;
    }
}
